﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KGE;
using KGE.Define;

namespace KGE.Tools
{
    public partial class SceneMaker : Form
    {
                Size tileSize = new Size(224, 120);
                bool StartPointed = false;
                //Rectangle rectBuff=new Rectangle();
        Point get_loc(Point p)
        {
            Point pp = p;

            p.X /= tileSize.Width;
            if (p.X < 0)
            {
                p.X--;
            }
            else if (p.X == 0)
            {
                if (pp.X < 0)
                {
                    p.X--;
                }
            }
            p.X *= tileSize.Width;

            p.Y /= tileSize.Height;
            if (p.Y < 0)
            {
                p.Y--;
            }
            else if (p.Y == 0)
            {
                if (pp.Y < 0)
                {
                    p.Y--;
                }
            }
            p.Y *= tileSize.Height;
            return p;
        }

        Game game;
        SceneMakerCOM sceneMakerCOM;
        int kip_index = -1;
        int ki_index = -1;
        Point offset;
        Point soffset;

        public SceneMaker()
        {
            InitializeComponent();
        }
        private void SceneMaker_Load(object sender, EventArgs e)
        {
            game = new Game(pictureBox1);
            sceneMakerCOM = new SceneMakerCOM(game);
            game.MountGameCOM(sceneMakerCOM);
            game.Start();

            gui_kiplist.Items.Clear();
            gui_kilist.Items.Clear();

            //for (int i = 0; i < game.KImagePackages.Count;i++ )
            //{
            //    gui_kiplist.Items.Add(game.KImagePackages[i].Name);
            //}

            //gui_layerlist.Items.Clear();
            //for (int i = 0; i < sceneMakerCOM.scene.LayerCount; i++)
            //{
            //    gui_layerlist.Items.Insert(0,i);
            //    gui_layerlist.SetItemChecked(0, true);
            //}
            //gui_layerlist.SetSelected(sceneMakerCOM.scene.LayerCount - 1, true);

            for (int i = 0; i < game.KImages.Count; i++)
            {
                gui_kiplist.Items.Add(game.KImages[i].Name);
            }

            for (int i = 0; i < game.KMovies.Count; i++)
            {
                gui_kilist.Items.Add(game.KMovies[i].Name);
            }
        }

        private void gui_kiplist_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = gui_kiplist.SelectedIndex;
            kip_index = i;
            //if (i < 0)
            //{
            //    return;
            //}
            //kip_index = i;
            //ki_index = 0;
            //gui_kilist.Items.Clear();
            //for (int j = 0; j < game.KImagePackages[kip_index].Count; j++)
            //{
            //    gui_kilist.Items.Add(j.ToString("d3"));
            //}
        }

        private void gui_kilist_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = gui_kilist.SelectedIndex;
            ki_index = i;
            //if (i < 0)
            //{
            //    return;
            //}
            //ki_index = i;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (kip_index < 0 && ki_index < 0)
            {
                return;
            }
            //sceneMakerCOM.mouseKI = game.KImagePackages[kip_index, ki_index];
            sceneMakerCOM.mouseKI = game.KImages[kip_index];
            sceneMakerCOM.mouseKM = game.KMovies[ki_index];
            gui_info.Text = "FPS: " + game.FPS.ToString("f0");
            //Point p = Util.PointSubPoint(sceneMakerCOM.mousePoint, sceneMakerCOM.camera);
            gui_info.Text = gui_info.Text + "  坐标: " + sceneMakerCOM.mousePoint.ToString();
            gui_info.Text = gui_info.Text + "  镜头: " + sceneMakerCOM.camera.ToString();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (gui_isdrawing.Checked)
                {
                    sceneMakerCOM.mousePoint = Util.PointAddPoint(e.Location, sceneMakerCOM.camera);
                    //sceneMakerCOM.mousePoint = e.Location;
                    if (gui_layerlist.SelectedIndex == 2)
                    {
                        sceneMakerCOM.mousePoint = get_loc(sceneMakerCOM.mousePoint);
                    }
                    sceneMakerCOM.drawMouse = true;
                }
                else
                {
                    //soffset = e.Location;
                }
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (gui_isdrawing.Checked)
                { }
                else
                {
                    soffset = e.Location;
                }
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            //sceneMakerCOM.drawMouse = false;
            if (e.Button == MouseButtons.Left)
            {
                if (gui_isdrawing.Checked)
                { }
                else
                {
                    offset.X = e.X - soffset.X;
                    offset.Y = e.Y - soffset.Y;
                    sceneMakerCOM.camera = Util.PointSubPoint(sceneMakerCOM.camera, offset);
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (gui_isdrawing.Checked)
                {
                    if (sceneMakerCOM.isMovie == false)
                    {
                        int layer_index = gui_layerlist.SelectedIndex;
                        Scene.Layer.Tile tile = new Scene.Layer.Tile();

                        tile.DrawRectangle.Location = sceneMakerCOM.mousePoint;
                        tile.DrawRectangle.Size = sceneMakerCOM.mouseKI.DrawRectangle.Size;
                        tile.KMovieIndexer = null;
                        tile.KImageIndexer = new ObjectIndexer(game.KImages[kip_index].Name);
                        sceneMakerCOM.scene[layer_index].Add(tile);
                        update_tilelist(gui_layerlist.SelectedIndex);
                    }
                    else
                    {
                        int layer_index = gui_layerlist.SelectedIndex;
                        Scene.Layer.Tile tile = new Scene.Layer.Tile();

                        tile.DrawRectangle.Location = sceneMakerCOM.mousePoint;
                        tile.DrawRectangle.Size = sceneMakerCOM.mouseKM.Size;
                        tile.KImageIndexer = null;
                        tile.KMovieIndexer = new ObjectIndexer(game.KMovies[ki_index].Name);
                        sceneMakerCOM.scene[layer_index].Add(tile);
                        update_tilelist(gui_layerlist.SelectedIndex);
                    }
                    sceneMakerCOM.drawMouse = false;
                }
                else
                {
                        sceneMakerCOM.mousePoint = Util.PointAddPoint(e.Location, sceneMakerCOM.camera);
                    if (StartPointed == false)
                    {
                        sceneMakerCOM.mouseZone.Location = sceneMakerCOM.mousePoint;
                        StartPointed = true;
                    }
                    else
                    {
                        sceneMakerCOM.mouseZone.Width = sceneMakerCOM.mousePoint.X - sceneMakerCOM.mouseZone.X;
                        sceneMakerCOM.mouseZone.Height = sceneMakerCOM.mousePoint.Y - sceneMakerCOM.mouseZone.Y;
                        StartPointed = false;
                    }
                }
            }
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            save();
        }
        void save()
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.RestoreDirectory = true;
            sfd.Filter = "Kevin Scene|*.KScene";
            if (sfd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            sceneMakerCOM.resetScene();
            Util.SaveObject(sfd.FileName, sceneMakerCOM.scene);
        }
        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.RestoreDirectory = true;
            fd.Filter = "Kevin Scene|*.KScene";
            if (fd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            open(fd.FileName);
        }
        public void open(string fn)
        {
            sceneMakerCOM.scene = (Scene)Util.LoadObject(fn);
        }

        private void gui_layerlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = gui_layerlist.SelectedIndex;
            update_tilelist(i);
        }

        void update_tilelist(int i)
        {
            gui_tilelist.Items.Clear();
            for (int j = 0; j < sceneMakerCOM.scene[i].TileCount; j++)
            {
                gui_tilelist.Items.Add(j);
            }
        }

        private void gui_tilelist_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void gui_deltile_Click(object sender, EventArgs e)
        {
            try
            {
                sceneMakerCOM.scene[gui_layerlist.SelectedIndex].Del(int.Parse(gui_tilelist.SelectedItem.ToString()));
                update_tilelist(gui_layerlist.SelectedIndex);
                gui_tilelist.SetSelected(0, true);
            }
            catch { }
        }
        private void gui_kilist_MouseEnter(object sender, EventArgs e)
        {
            sceneMakerCOM.isMovie = true;
        }

        private void gui_kiplist_MouseEnter(object sender, EventArgs e)
        {
            sceneMakerCOM.isMovie = false;
        }

        private void gui_add_zone_Click(object sender, EventArgs e)
        {
            if (StartPointed == false)
            {
                sceneMakerCOM.scene.Add(sceneMakerCOM.mouseZone);
            }
        }
    }
}
